package com.ap.core.bulkupload.provider.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ap.core.bulkupload.jaxb.domain.Merchant;
import com.ap.core.bulkupload.jaxb.domain.MerchantAddressType;
import com.ap.core.bulkupload.types.MerchantCtgyCode;
import com.ap.core.bulkupload.provider.MerchantRegistrationServiceProvider;
import com.ap.core.bulkupload.service.ManageMerchantService;
import com.ap.core.bulkupload.translator.MsgHdrTypeTranslator;
import com.ap.core.cxf.jaxb.common.AddressMandatoryCountryCode;
import com.ap.core.cxf.jaxb.common.RequestTypeCode;
import com.ap.core.cxf.jaxb.managemerchant.CreditActType;
import com.ap.core.cxf.jaxb.managemerchant.MerchantInfoType;
import com.ap.core.cxf.jaxb.managemerchant.RegisterMerchantMessageType;
import com.ap.core.cxf.jaxb.managemerchant.RequestMerchantRegistrationType;
import com.ap.core.cxf.jaxb.managemerchant.ResponseMerchantRegistrationType;
import com.ap.core.cxf.jaxb.msghdr.MessageType;
import com.ap.core.exception.APCoreException;

/**
 * The Class MerchantRegistrationServiceProvider.
 * @author Siva.Pochana
 */
public class MerchantRegistrationServiceProviderImpl implements MerchantRegistrationServiceProvider {

	/** The Constant LOG. */
	private static final Logger LOG = LoggerFactory.getLogger(MerchantRegistrationServiceProviderImpl.class);
	
	/** The manage merchant service. */
	private ManageMerchantService manageMerchantService;
	
	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.provider.MerchantRegistrationServiceProvider#registerMerchant(java.lang.String, com.ap.core.bulkupload.jaxb.domain.Merchant)
	 */
	@Override
	public boolean registerMerchant(String distributorId, Merchant merchant) {
		String methodName = "registerMerchant () -";
		boolean isMerchantRegistered = false;
		try {
			LOG.info(methodName + "Register a merchant invoking with distributorId ["+distributorId+"]");
			
			RequestMerchantRegistrationType merchantRegRequest = this.populateRequest(distributorId, merchant);
			
			ResponseMerchantRegistrationType mercahntRegResponse = getManageMerchantService().registerMerchant(merchantRegRequest);
			
			if(mercahntRegResponse == null || mercahntRegResponse.getMerchantRegistResponse() == null) {
				LOG.error(methodName + "Unable to register merchant ["+merchant.getMerchantName()+"] for Distributor Id ["+distributorId+"]");
			} else {
				LOG.info(methodName + "Merchant is registered successfully...!!!");
				isMerchantRegistered = true;
			}
		} catch (APCoreException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return isMerchantRegistered;
	}
	
	
	/**
	 * Populate request.
	 *
	 * @param distributorId the distributor id
	 * @param merchant the merchant
	 * @return the request merchant registration type
	 * @throws APCoreException the aP core exception
	 */
	private RequestMerchantRegistrationType populateRequest(String distributorId, Merchant merchant) throws APCoreException {
		String methondName = "populateRequest() -";
		RequestMerchantRegistrationType merchantRequest = new RequestMerchantRegistrationType();
		
		try {
			LOG.info(methondName + "Merchant registration request population");
			merchantRequest.setMsgHdr(MsgHdrTypeTranslator.buildMsgHdrType(RegisterMerchantMessageType.ADDMERCHANT));
			merchantRequest.setDistributorId(distributorId);
			
			MerchantInfoType merchantInfoType = new MerchantInfoType();
			merchantInfoType.setMerchantId(merchant.getMerchantId());
			merchantInfoType.setMerchantName(merchant.getMerchantName());
			merchantInfoType.setMerchantGroupId(merchant.getMerchantGroupId());
			merchantInfoType.setMerchantLogo(merchant.getMerchantLogo());
			merchantInfoType.setMerchantCmnName(merchant.getMerchantCmnName());
			merchantInfoType.setUpdateReason(merchant.getUpdateReason());
			
			/* set merchant category values */
			for(MerchantCtgyCode code : MerchantCtgyCode.values()) {
				if(code.getMerchantCtgyCode().equals(merchant.getMerchantCtgyCd().value())) {
					merchantInfoType.setMerchantCtgyCd(code.getMerchantCtgyCode());
				}
			}
			
			/* set merchant request type */
			for(RequestTypeCode code : RequestTypeCode.values()) {
				if(code.value().equals(merchant.getRequestType().value())) {
					merchantInfoType.setRequestType(code);
				}
			}
			
			/* set mercahnt address */
			merchantInfoType.setMerchantAddress(this.populateAddress(merchant.getMerchantAddress()));
			if(merchant.getCreditAct() != null) {
				CreditActType creditAct = new CreditActType();
				creditAct.setCreditActNum(merchant.getCreditAct().getCreditActNum());
				creditAct.setCreditSortCode(merchant.getCreditAct().getCreditSortCode());
				creditAct.setRollNumber(merchant.getCreditAct().getRollNumber());
				
				merchantInfoType.setCreditAct(creditAct);
			}
			
			merchantRequest.setMerchant(merchantInfoType);

			LOG.info(methondName + "Merchant registration request populated");
			
			return merchantRequest;
	
		} catch (APCoreException e) {
			LOG.error(methondName + "APCoreException ocurred :"+e.getMessage());
			throw e;
		} catch (Exception e) {
			LOG.error(methondName + "Exception ocurred :"+e.getMessage());
			throw new APCoreException(e);
		}
	}	
		
	
	/**
	 * Populate address.
	 *
	 * @param merAddress the mer address
	 * @return the address mandatory country code
	 */
	private AddressMandatoryCountryCode populateAddress(MerchantAddressType merAddress) {
		
		AddressMandatoryCountryCode address = new AddressMandatoryCountryCode();
		address.setAddressLine1(merAddress.getAddressLine1());
		address.setAddressLine2(merAddress.getAddressLine2());
		address.setAddressLine3(merAddress.getAddressLine3());
		address.setAddressLine4(merAddress.getAddressLine4());
		address.setAddressLine5(merAddress.getAddressLine5());
		address.setAddressLine6(merAddress.getAddressLine6());
		address.setPostCode(merAddress.getPostCode());
		address.setCountryCode(merAddress.getCountryCode());
		
		return address;
	}

	/* (non-Javadoc)
	 * @see com.ap.core.bulkupload.provider.MerchantRegistrationServiceProvider#setManageMerchantService(com.ap.core.bulkupload.service.ManageMerchantService)
	 */
	@Override
	public void setManageMerchantService(ManageMerchantService manageMerchantService) {
		this.manageMerchantService = manageMerchantService;
	}


	/**
	 * @return the manageMerchantService
	 */
	public ManageMerchantService getManageMerchantService() {
		return manageMerchantService;
	}
	
	
}
